<!--
 * @Descripttion: 群组信息
 * @Author: 银河以北
 * @Date: 2021-10-07 09:52:18
 * @LastEditors: 银河以北
 * @LastEditTime: 2021-10-07 14:01:41
-->
<template>
  <div class='group'>
    <div class='group-title'>
      <el-divider content-position="left">{{groupData.group_chat_name}} ({{groupData.GroupChatUserInfo?.length}})</el-divider>
    </div>
    <div class='group-user'>
      <div
        class='group-user-item'
        v-for="(item,index) in groupData.GroupChatUserInfo"
        :key='index'
      >
        <el-badge
          :value="'群主'"
          class="item"
          type="warning"
          :hidden='item.userInfo.id!=groupData.user_id'
        >
          <el-avatar
            :size="50"
            :src="$utils.imgUrl(item.userInfo.avatar_url)"
          ></el-avatar>
        </el-badge>
        <p
          class='group-user-item-name'
          :class='{"group-user-item-active-name":item.userInfo.id==groupData.user_id}'
        >{{item.userInfo.nickname}}</p>
      </div>
    </div>
    <div
      class='group-title'
      v-if="groupData.user_id==userInfo.id"
    >
      <el-divider content-position="left">群组操作</el-divider>
    </div>
    <div
      class='group-operation'
      v-if="groupData.user_id==userInfo.id"
    >
      <el-link
        type="danger"
        :underline='false'
        @click="deleteGroupChat(groupData)"
      >解散群组</el-link>
    </div>
  </div>
</template>

<script lang="ts">
import { computed, defineComponent, reactive, ref, watch, nextTick } from "vue";

//vuex
import { useStore } from "vuex";

import { ElMessageBox, ElMessage } from "element-plus";

import { deleteUserGroupChatItem } from "@/api/chat/index";
export default defineComponent({
  name: "GroupInfo",
  props: {
    groupInfo: {
      type: Object,
      default: () => {},
    },
  },
  setup(props, { emit }) {
    //引入vuex
    const store = useStore();

    let userInfo: any = reactive({});
    userInfo = computed(() => {
      return store.getters.userInfo;
    });

    /**
     * 群组数据
     */
    let groupData: any = reactive({
      group_chat_name: "",
      GroupChatUserInfo: [],
    });
    groupData = computed(() => {
      return props.groupInfo;
    });

    //解散群组
    const deleteGroupChat = (item: any) => {
      ElMessageBox.confirm(
        "你确定要执行这次操作吗，解散群组后无法进行恢复！",
        "解散群组",
        {
          confirmButtonText: "我确定",
          cancelButtonText: "我再考虑考虑",
          type: "warning",
        }
      )
        .then(() => {
          const data = { groupId: item.id };
          deleteUserGroupChatItem(data).then((res: any) => {
            if (res.code == 200) {
              emit("updateGroupList");
              ElMessage({
                type: "success",
                message: "删除成功",
              });
            }
          });
        })
        .catch(() => {
          ElMessage({
            type: "info",
            message: "取消删除",
          });
        });
    };
    return {
      groupData, //群组数据

      userInfo, //用户信息

      deleteGroupChat, //删除群组
    };
  },
});
</script>
<style lang="scss" scoped>
.group {
  width: 100%;
  &-title {
    color: #000;
  }
  &-user {
    display: flex;
    justify-content: flex-start;
    flex-wrap: wrap;
    align-items: center;
    &-item {
      width: 50px;
      height: 60px;
      margin: 5px;
      &-name {
        width: 50px;
        font-size: 12px;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
      }
      &-active-name {
        font-weight: 600;
        color: #5cadff;
      }
    }
  }
}
</style>